#include "exmsg.h"
#include "sys.h"
#include "stdtypes.h"
#include "fix_queue.h"



static void *msg_tbl[EXMSG_MSG_CNT];
fix_queue_t msg_queue;
// static exmsg_t msg_buffer[EXMSG_MSG_CNT];

net_err_t exmsg_init(void) {
    LOG_DEBUG("exmsg init\n");
    fix_queue_init(&msg_queue, msg_tbl, EXMSG_MSG_CNT);
    return NET_ERR_OK;
}

net_err_t exmsg_netif_in(void) {
    exmsg_t *msg = mnet_malloc(sizeof(exmsg_t));
    msg->msg_type = NET_EXMSG_NETIF_IN;
    memcpy(msg->data, "it is net msg", 13);
    // LOG_DEBUG("msg data is: %s\n", msg->data);
    fix_queue_send(&msg_queue, (void *)msg, 200);
}

static void work_thread(void *arg) {
    LOG_DEBUG("exmsg is running...\n");
    while (1) {
        // void *msg = fix_queue_recv(&msg_queue, 1000);
        // if (msg != (void *)0) {
        //     LOG_INFO("Recv data: %s\n", ((exmsg_t *)msg)->data);
        //     mnet_free((void *)msg);
        // }
        // sys_sleep(10000);
    }
}

net_err_t exmsg_start(void) {
    sys_thread_t thread = sys_thread_create(work_thread, (void *)0);
    if (thread == SYS_THREAD_INVALID) {
        LOG_DEBUG("thread create fail!\n");
        return NET_ERR_SYS;
    }
    return NET_ERR_OK;
}